#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>

/*
	mynode@0x12345678{
		compatible = "hqyj, mynode";
		astring = "i am ok";
		uint = <0x11223344>;
		binint = [00 0c 29 78 f8 be];
		mixed = "dc2022", <0x8899aabb>, [01 02 03 04];
	};
*/

MODULE_LICENSE("GPL");

static struct device_node* node;

static int __init dt_demo_init(void)
{
    int returnLength, i;
    struct property* findProperties;

    const char* strVal;
    char u8Array[6] = {0};


    node = of_find_node_by_path("/mynode@0x12345678");
    if(!node)
    {
        printk("of_find_node_by_path(\"/mynode@0x12345678\") is not find node");
        printk("not find spec node!\n");
        return -ENODEV;
    }

    printk("node name = %s, full name = %s\n", node->name, node->full_name);

    printk("=================<properties>===================\n");
    findProperties = node->properties;
    for(; findProperties; findProperties = findProperties->next)
    {
        printk("name=%s, length=%d\n", findProperties->name, findProperties->length);
    }

    findProperties = of_find_property(node,"binint",&returnLength);
    if(findProperties)
    {
        short* tmpValue = (short*)findProperties->value;
        printk("name=%s, length=%d, value=[%#2X %#2X %#2X %#2X %#2X %#2X]",
        findProperties->name,returnLength,
        *(tmpValue),*(tmpValue+1),*(tmpValue+2),*(tmpValue+3),*(tmpValue+4),*(tmpValue+5));
    }
        
    printk("==================<values>====================\n");
    findProperties = node->properties;
    printk("name = %s, length = %d, value = %s\n", findProperties->name, findProperties->length, (char*)findProperties->value);
    findProperties = findProperties->next;
    printk("name = %s, length = %d, value = %s\n", findProperties->name, findProperties->length, (char*)findProperties->value);
    findProperties = findProperties->next;
    printk("name = %s, length = %d, value = %#x\n", findProperties->name, findProperties->length, __be32_to_cpup((int*)findProperties->value));
    findProperties = findProperties->next;
    for(i = 0; i < findProperties->length; i++)
    {
        printk("name = %s, length = %d, value = %#2x\n", findProperties->name, findProperties->length, ((char*)(findProperties->value))[i]);
    }
    findProperties = findProperties->next;
    printk("name = %s, length = %d, value = %#x\n", findProperties->name, findProperties->length, __be32_to_cpup((int *)(findProperties->value+7)));

    of_property_read_string_index(node, "astring", 0, &strVal);
    printk("read string by api %s\n", strVal);
    of_property_read_u8_array(node, "binint", u8Array, 6);
    printk("read u8 by api:");for(i = 0; i < 6; i++) printk("%#2x ", u8Array[i]);
    return 0;
}

static void __exit dt_demo_exit(void)
{
    of_node_put(node);
}

module_init(dt_demo_init);
module_exit(dt_demo_exit);